package com.phuoc.activities;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.RatingBar;
import android.widget.TextView;

import com.phuoc.dao.TypeOfEstablishmentDAO;
import com.phuoc.entities.Establishment;

public class EstablishmentAdapter extends ArrayAdapter<Establishment> {

	private Context context;
	private List<Establishment> list;
	private TypeOfEstablishmentDAO typeEstabDataSource;
	private ArrayList<String> listIdSelected;
	private boolean hideCheckBox = false;

	public EstablishmentAdapter(Context context, int textViewResourceId,
			List<Establishment> objects, boolean hideCheckBox) {
		super(context, textViewResourceId, objects);
		this.context = context;
		this.list = objects;
		this.hideCheckBox = hideCheckBox;

		listIdSelected = new ArrayList<String>();

		// Initial DAO
		typeEstabDataSource = new TypeOfEstablishmentDAO(this.context);

	}

	public ArrayList<String> getListIdSelected() {
		return listIdSelected;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		Establishment item = list.get(position);

		// The child view in each row
		CheckBox cbId;
		TextView txtName;
		TextView txtTypeOfEstab;
		TextView txtCost;
		RatingBar rbOverall;

		// Create new row view
		if (v == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.item, null);

			// Find child view
			cbId = (CheckBox) v.findViewById(R.id.cbId);
			if (hideCheckBox)
				cbId.setVisibility(CheckBox.VISIBLE);
			else
				cbId.setVisibility(CheckBox.GONE);
			txtName = (TextView) v.findViewById(R.id.txtNameEstab);
			txtTypeOfEstab = (TextView) v.findViewById(R.id.txtTypeOfEstab);
			txtCost = (TextView) v.findViewById(R.id.txtCost);
			rbOverall = (RatingBar) v.findViewById(R.id.rbOverall);

			v.setTag(new EstabViewHolder(cbId, txtName, txtTypeOfEstab,
					rbOverall, txtCost));

			cbId.setOnClickListener(new OnClickListener() {
				public void onClick(View view) {
					CheckBox cb = (CheckBox) view;
					Establishment estab = (Establishment) cb.getTag();
					estab.setChecked(cb.isChecked());
					if (cb.isChecked())
						listIdSelected.add(estab.getId());
					else
						listIdSelected.remove(estab.getId());
				}
			});
		} else {
			EstabViewHolder viewHolder = (EstabViewHolder) v.getTag();
			cbId = viewHolder.getIdCheckBoxView();
			txtName = viewHolder.getNameView();
			txtTypeOfEstab = viewHolder.getTypeEstabView();
			txtCost = viewHolder.getCostView();
			rbOverall = viewHolder.getRatingView();
		}

		// Tag the CheckBox with the Planet it is displaying, so that we can
		// access the planet in onClick() when the CheckBox is toggled.
		cbId.setTag(item);

		// Display data
		typeEstabDataSource.open();
		//cbId.setText(item.getId());
		cbId.setChecked(item.isChecked());
		// Log.d("id", item.getId());
		// Log.d("check", Boolean.toString(item.isChecked()));
		txtName.setText(item.getName());
		txtTypeOfEstab.setText(typeEstabDataSource.getNameById(item
				.getIdTypeOfEstablishment()));
		if (item.getCost() == 0) {
			txtCost.setText("-- US$");
		} else {
			txtCost.setText(Float.toString(item.getCost()) + " US$");
		}
		rbOverall.setRating(item.getRateOverall());
		typeEstabDataSource.close();

		return v;
	}

	private static class EstabViewHolder {
		private CheckBox idCheckBoxView;
		private TextView nameView;
		private TextView typeOfEstabView;
		private RatingBar ratingView;
		private TextView costView;

		public EstabViewHolder(CheckBox _idCheckBoxView, TextView _nameView,
				TextView _typeOfEstabView, RatingBar _ratingView,
				TextView _costView) {
			this.idCheckBoxView = _idCheckBoxView;
			this.nameView = _nameView;
			this.typeOfEstabView = _typeOfEstabView;
			this.ratingView = _ratingView;
			this.costView = _costView;
		}

		public TextView getNameView() {
			return this.nameView;
		}

		public TextView getTypeEstabView() {
			return this.typeOfEstabView;
		}

		public RatingBar getRatingView() {
			return this.ratingView;
		}

		public TextView getCostView() {
			return this.costView;
		}

		public CheckBox getIdCheckBoxView() {
			return idCheckBoxView;
		}
	}
}
